package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.exception.BusinessException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.IOrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrdersOverVO;
import com.sky.vo.OrdersSubmitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController(value = "userOrderController")
@RequestMapping("/user/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    /**
     * 提交订单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    public Result<OrdersSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        OrdersSubmitVO ordersSubmitVO=orderService.submit(ordersSubmitDTO);
        return Result.success(ordersSubmitVO);
    }

    /**
     * 查看历史订单
     * @param page
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/historyOrders")
    public Result<PageResult<OrdersOverVO>> historyOrders(@RequestParam Integer page,
                                                          @RequestParam Integer pageSize,
                                                          @RequestParam(required = false) Integer status){
        PageResult<OrdersOverVO> ordersOverVOPageResult=orderService.historyOrders(page,pageSize,status);
        return Result.success(ordersOverVOPageResult);
    }

    /**
     * 订单的支付
     * @param ordersPaymentDTO
     * @return
     * @throws Exception
     */
    @PutMapping("/payment")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO)throws Exception{
        OrderPaymentVO orderPaymentVO=orderService.payment(ordersPaymentDTO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 查询历史订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    public Result<OrdersOverVO> orderDetail(@PathVariable(value = "id")Long id){
        OrdersOverVO ordersOverVO=orderService.orderDetailById(id);
        if (ordersOverVO==null){
            throw new BusinessException("测试输入的值不合法");
        }else {
            return Result.success(ordersOverVO);
        }
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    public Result<String> cancel(@PathVariable(value = "id") Long id){
        orderService.cancel(id);
        return Result.success("取消订单成功");
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    public Result<String> repetition(@PathVariable(value = "id")Long id){
        orderService.repetition(id);
        return Result.success();
    }
}
